using System.Diagnostics;
using AutoMapper;
using EFDemo;
using EFDemo.Mapper;
using EFDemo.Model;
using Microsoft.VisualStudio.TestPlatform.TestHost;

namespace TestProject1;

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void InitData()
    {
        using (MyContext dbContext=new MyContext())
        {
            EFDemo.Program.InitData(dbContext);
            dbContext.Dispose();
        }
    }
    [TestMethod]
    public void CreateStudent()
    {
        using (MyContext dbContext=new MyContext())
        {
            var model = new StuModel();
            model.Name = "xxx";
            model.Password = "123";
            
            //创建配置对象
            var configuration = new MapperConfiguration(cfg =>
            { 
                cfg.AddProfile<StuMapper>();
            });
            //创建映射对象
            var mapper = configuration.CreateMapper();
            var dest = mapper.Map<Student>(model);
            dbContext.Set<Student>().Add(dest);
            dbContext.SaveChanges();
            Debug.WriteLine(dest.ToString());
        }
    }
    [TestMethod]
    public void TestMethod1()
    {
        using (MyContext dc = new MyContext())
        {
            var student3 = dc.Set<Student>().Select(x => new
            {
                name = x.Name,
                majors = string.Join(',', x.StudentMajor.Select(y => y.Major.MajorCode))
            }).ToList();
            Console.WriteLine("3. 对查询字段进行简单处理，比如将多行数据转为一行逗号分隔的数据");
            Console.WriteLine();
            var student4 = dc.Set<Student>().Where(x => x.StudentMajor.Select(y => y.Major.School.SchoolName).Contains("School1")).ToList();
            Console.WriteLine("4. 简单查询   在联表中使用查询条件");
            Console.WriteLine();
            
        }
    }
}